package cn.zyb.pet.service.impl;

import cn.zyb.basic.exception.BusinessException;
import cn.zyb.basic.service.IBaiduAiAuditService;
import cn.zyb.basic.service.Impl.BaseServiceImpl;
import cn.zyb.basic.util.DistanceUtils;
import cn.zyb.basic.util.LoginContext;
import cn.zyb.basic.util.PageList;
import cn.zyb.basic.util.Point;
import cn.zyb.org.domain.Employee;
import cn.zyb.org.domain.Shop;
import cn.zyb.org.mapper.EmployeeMapper;
import cn.zyb.org.mapper.ShopMapper;
import cn.zyb.pet.domain.SearchMasterMsg;
import cn.zyb.pet.domain.SearchMasterMsgAuditLog;
import cn.zyb.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zyb.pet.mapper.SearchMasterMsgMapper;
import cn.zyb.pet.query.SearchMasterMsgQuery;
import cn.zyb.pet.service.ISearchMasterMsgService;
import cn.zyb.user.domain.Logininfo;
import cn.zyb.user.domain.User;
import cn.zyb.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
  @Autowired
  private UserMapper userMapper;

  @Autowired
  private SearchMasterMsgMapper searchMasterMsgMapper;

  @Autowired
  private IBaiduAiAuditService baiduAiAuditService;

  @Autowired
  private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

  @Autowired
  private ShopMapper shopMapper;

  @Autowired
  private EmployeeMapper employeeMapper;


  @Override
  @Transactional
  public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
    //        name: '小七',
    //        price: 10,
    //        age: 3,
    //        gender: 1,
    //        coatColor: '黑色',
    //        resources: null,
    //        petType: 1,
    //        address: null,
    //        title: '小七寻主'
    //      id - 自增长 - 不用管
    //      state = 1[审核成功待处理 ，如果审核失败state=0] - 审核通过才设置
    //      user_id[LoginContext工具类]
    //      shop_id[分配给店铺的时候才有值 - 审核成功分给给店铺才有。也有可能50公里内没有，没有就设置为null] - 审核通过才设置

    //业务：
    //1.添加寻主消息到数据库【获取用户的id】
    Logininfo logininfo = LoginContext.getLogininfo(request);
    User user = userMapper.loadByLogininfoId(logininfo.getId());
    searchMasterMsg.setUserId(user.getId());
    searchMasterMsgMapper.save(searchMasterMsg);


    //2.审核 - 百度AI智能审核，没有人工审核 - BaiduAiAuditServiceImpl的方法
    //a.对name和address进行内容审核
    //b.对resources进行图片审核
    Boolean boo1 = baiduAiAuditService.textAudit(searchMasterMsg.getName() + searchMasterMsg.getAddress());
    Boolean boo2 = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());


    //3.审核通过
    if(boo1 && boo2){

      //b.添加审核日志
      SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
      log.setState(1);
      log.setMsgId(searchMasterMsg.getId());
      //  audit_id   --  自动审核没有审核人 - 系统
      log.setNote("审核成功");
      searchMasterMsgAuditLogMapper.save(log);

      //c.获取当前用户最近的店铺，将该寻主消息分配给这个店铺 - 工具类
      Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
      //注意：t_shop中的地址一定要真实，而且不能为null
      Shop nearestShop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());
      //d.给店铺管理员发邮件或发短信通知它到指定的address去收购宠物
      if(nearestShop!=null){
//                SmsUtils.sendSms(nearestShop.getTel(),"你有新的订单，请到"+searchMasterMsg.getAddress()+"去接宠物");
        System.out.println("你有新的订单，请到"+searchMasterMsg.getAddress()+"去接宠物");
      }
      //e.如果没有这个店铺 - 不用管

      //a.修改状态state=1【审核成功待处理】
      searchMasterMsg.setState(1);
      if(nearestShop!=null){//找到了用户附近50公里以内的店铺
        searchMasterMsg.setShopId(nearestShop.getId());
      }
      searchMasterMsgMapper.update(searchMasterMsg);

    }else{
      //4.审核失败 - 设置状态state=0【待审核=审核失败】，可以使用默认值 + 添加审核日志
      SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
      log.setMsgId(searchMasterMsg.getId());
      //  audit_id   --  自动审核没有审核人 - 系统
      log.setNote("审核失败，内容非法");
      log.setState(0);
      searchMasterMsgAuditLogMapper.save(log);
      throw new BusinessException("审核失败，内容非法");
    }

  }

  @Override
  public PageList<SearchMasterMsg> userQuery(SearchMasterMsgQuery query, HttpServletRequest request) {
    //获取用户的id
    Logininfo logininfo = LoginContext.getLogininfo(request);
    User user = userMapper.loadByLogininfoId(logininfo.getId());
    query.setUserId(user.getId());
    return super.queryPage(query);
  }

  @Override
  public PageList<SearchMasterMsg> Tohandle(SearchMasterMsgQuery query, HttpServletRequest request) {
    //判断一个管理员是店铺管理员还是平台管理员
    Logininfo logininfo = LoginContext.getLogininfo(request);
    Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
    //如果是店铺管理员就设置店铺id
    if(employee.getShop_id()!=null){//当前管理员是店铺管理员
      query.setShopId(employee.getShop_id());
    }
    return super.queryPage(query);
  }
}
